﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace ShenLanCMS.Models
{
    /// <summary>
    /// 文章实体
    /// </summary>
    public class Post
    {
        [System.ComponentModel.DataAnnotations.Key]
        public Guid PostID { get; set; }

        /// <summary>
        /// 作者
        /// </summary>
        [Display(Name = "作者")]
        public Guid AddUserID { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        [Display(Name = "标题")]
        [Required(ErrorMessage = "请填写{0}")]
        [StringLength(128, ErrorMessage = "{0}不能超过{1}个字符")]
        public string Title { get; set; }

        /// <summary>
        /// 分类id
        /// </summary>
        [Display(Name = "分类")]
        public int PostTypeID { get; set; }

        /// <summary>
        /// 标签
        /// </summary>
        [Display(Name = "标签")]
        [StringLength(64, ErrorMessage = "标签不能超过{1}个字符")]
        public string Tag { get; set; }

        /// <summary>
        /// 关键词
        /// </summary>
        [Display(Name = "关键词")]
        [StringLength(64, ErrorMessage = "关键词不能超过{1}个字符")]
        public string Keyword { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        [Display(Name = "描述")]
        [DataType(DataType.MultilineText)]
        [StringLength(255, ErrorMessage = "描述不能超过{1}个字符")]
        public string Description { get; set; }

        /// <summary>
        /// 发表时间
        /// </summary>
        [Display(Name = "发表时间")]
        public DateTime PubTime { get; set; }

        /// <summary>
        /// 文章内容
        /// </summary>
        [Display(Name = "文章内容")]
        [Required(ErrorMessage = "请填写{0}")]
        [DataType(DataType.MultilineText)]
        [StringLength(100000000, ErrorMessage = "{0}不能超过{1}个字符")]
        public string Content { get; set; }

        /// <summary>
        /// 顶
        /// </summary>
        [Display(Name = "顶")]
        public int DigGood { get; set; }

        /// <summary>
        /// 踩
        /// </summary>
        [Display(Name = "踩")]
        public int DigBad { get; set; }

        /// <summary>
        /// 点击量
        /// </summary>
        [Display(Name = "点击量")]
        public int Click { get; set; }

        /// <summary>
        /// 文件名:用于生成静态
        /// </summary>
        [Display(Name = "文件名")]
        [StringLength(128, ErrorMessage = "{0}不能超过{1}个字符")]
        public string FileName { get; set; }

        /// <summary>
        /// 权重
        /// </summary>
        [Display(Name = "权重")]
        public int? Weight { get; set; }

        /// <summary>
        /// 状态:0为回收站,1为正常,2为草稿
        /// </summary>
        [Display(Name = "状态")]
        public int Status { get; set; }

        public Post()
        {
        }

        EntityContext db = new EntityContext();

        /// <summary>
        /// 获取文章作者信息
        /// </summary>
        /// <returns></returns>
        public User GetUser(){
            var user = db.Users.SingleOrDefault(n => n.UserID == this.AddUserID);
            if (user == null)
                user = new User();
            return user;
        }

        /// <summary>
        /// 获取文章分类信息
        /// </summary>
        /// <returns></returns>
        public PostType GetPostType() {
            var pt = db.PostTypes.SingleOrDefault(n => n.PostTypeID == this.PostTypeID);
            if (pt == null)
                pt = new PostType();
            return pt;
        }
    }
}
